#!/bin/bash

source ./run.sh

function replaceConfigFile(){
	cp -f /home/cskq/git_repo/urdu_config/fastfds/logback-spring.xml  /home/cskq/git_repo/Urdu_Logistics/fastdfs/target/classes/config;
	cp -f /home/cskq/git_repo/urdu_config/fastfds/application.properties /home/cskq/git_repo/Urdu_Logistics/fastdfs/src/main/resources;

	cp -f /home/cskq/git_repo/urdu_config/admin/logback-spring.xml  /home/cskq/git_repo/Urdu_Logistics/urdu_admin/target/classes/config;
	cp -f /home/cskq/git_repo/urdu_config/freight/logback-spring.xml  /home/cskq/git_repo/Urdu_Logistics/urdu_freight/target/classes/config;
	cp -f /home/cskq/git_repo/urdu_config/demandsite/logback-spring.xml  /home/cskq/git_repo/Urdu_Logistics/urdu_demandsite/target/classes/config;
	cp -f /home/cskq/git_repo/urdu_config/userServer/logback-spring.xml   /home/cskq/git_repo/Urdu_Logistics/urdu_user_server/target/classes/config;
	cp -f /home/cskq/git_repo/urdu_config/businessServer/logback-spring.xml  /home/cskq/git_repo/Urdu_Logistics/urdu_common_business_server/target/classes/config;
}

function menu (){
cat << EOF
------------------------------------------------------------
|***************Urdu Project Auto Deployment***************|
------------------------------------------------------------
`echo -e "\033[35m ucp)  update compile package urdu project \033[0m"`
`echo -e "\033[35m run)  选择服务运行 \033[0m"`
`echo -e "\033[35m exit) 退出\033[0m"`
EOF

	read -p "请输入对应操作名称：" num1
	case $num1 in
		ucp)
			 echo "update compile package project beginning 。。。。。。。"
		  	 cd /home/cskq/git_repo/Urdu_Logistics
		  	 git pull -u origin master:master
		  	 mvn clean compile
		  	 replaceConfigFile
		  	 mvn package -Dmaven.test.skip=true
		  	 echo "update compile package project end    。。。。。。。"
		  	 menu
		  	 ;;
		run)
		  	deploy_menu
		  	;;		 
		exit)
		  	exit 0
		 	;;
		*)
		  	echo -e "\033[41;33;5m $num1 不支持的操作 \033[0m"
		  	menu
	esac
}

function deploy_menu(){
cat << EOF
------------------------------------------------------------------------
|******** Runing  程序运行日志目录:/home/cskq/logs  *******************|
|********                                           *******************|
|******** 公共组件服务 name:commonapi  	port:8001   *******************|
|******** 后台服务     name:admin      	port:8002   *******************|
|******** 车主服务     name:freight    	port:8003   *******************|
|******** 货主服务     name:demandsite  port:8004   *******************|
|******** 用户服务     name:userservice port:8005   *******************|
|******** 公共业务服务 name:bzservice   port:8006   *******************|
------------------------------------------------------------------------
`echo -e "\033[35m start)  name services start   \033[0m"`
`echo -e "\033[35m stop)  name services stop   \033[0m"`
`echo -e "\033[35m restart)  name services restart \033[0m"`
`echo -e "\033[35m status)  name services status \033[0m"`
`echo -e "\033[35m back) 返回主菜单\033[0m"`
EOF
	read -p "请输入对应操作名称及服务名称(如:start  admin | stop admin) "  num2  APP_NAME
	case $num2 in
		start)
		  getAppInfo
		  echo -e "name:\033[42;37m $APP_NAME \033[0m  services start   \033[46;37m PORT: $APP_PORT \033[0m ...... "
		  start 
		  ;;
		stop)
		  getAppInfo
 	          echo -e "name:\033[41;37m $APP_NAME \033[0m  services stop   \033[41;37m PORT: $APP_PORT \033[0m ...... "
		  stop
		  ;;
		restart)
		  getAppInfo
		  echo -e "name:\033[42;37m $APP_NAME \033[0m  services restart   \033[46;37m PORT: $APP_PORT \033[0m ...... "
		  restart	
  		  ;;
                status)
		  getAppInfo
		  echo -e "name:\033[42;37m $APP_NAME \033[0m  services status   \033[46;37m PORT: $APP_PORT \033[0m ...... "
		  status
		  ;;
		back)
		  clear
		  menu
		  ;;
		*)
		  echo -e "\033[41;33;5m $num2 不支持的操作 \033[0m"
		  deploy_menu
	esac
}

function getAppInfo(){
    APP_PATH=""
    APP_PORT=8080
    case $APP_NAME in
		commonapi)
			APP_PATH="/home/cskq/git_repo/Urdu_Logistics/fastdfs/target/commonapi.jar";
			APP_PORT=8001
		    ;;
		admin)
			APP_PATH="/home/cskq/git_repo/Urdu_Logistics/urdu_admin/target/admin.jar";
			APP_PORT=8002
		    ;;
		freight)
			APP_PATH="/home/cskq/git_repo/Urdu_Logistics/urdu_freight/target/freight.jar";
			APP_PORT=8003
                    ;;
		demandsite)
			APP_PATH="/home/cskq/git_repo/Urdu_Logistics/urdu_demandsite/target/demandsite.jar";
			APP_PORT=8004
			;;		
		userservice)
			APP_PATH="/home/cskq/git_repo/Urdu_Logistics/urdu_user_server/target/userservice.jar";
			APP_PORT=8005
			;;		
		bzservice)
			APP_PATH="/home/cskq/git_repo/Urdu_Logistics/urdu_common_business_server/target/bzservice.jar";
			APP_PORT=8006
			;;
		*)
 			echo -e "\033[41;33;5m 不支持的服务名：$APP_NAME \033[0m"
			deploy_menu
	esac
	
	if [ ! -f "$APP_PATH" ]; then #文件不存在
            echo -e "\033[41;33;5m 【$num2  $APP_NAME】的服务文件不存在，请先执行 update compile package操作! \033[0m"
	    menu		
	fi
}
menu
